﻿/*************************************************************************
 * Copyright 2013, OhZeeCreative, LLC
 * This code is protected by copyright. Unauthorized use is prohibited.
 *************************************************************************/

namespace OhZeeCreative.Windows8.UI.Lynyrd
{
    using System.Windows.Input;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls.Primitives;

    public class OzToggleButton : ToggleButton
    {
        private object uncheckedContent;
        private ICommand uncheckedCommand;
        private Style uncheckedStyle;

        public static readonly DependencyProperty CheckedCommandProperty = DependencyProperty.Register("CheckedCommand", typeof(ICommand), typeof(OzToggleButton), null);
        public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.Register("CheckedContent", typeof(object), typeof(OzToggleButton), null);
        public static readonly DependencyProperty CheckedStyleProperty = DependencyProperty.Register("CheckedStyle", typeof(Style), typeof(OzToggleButton), null);
        
        public string CheckedContent
        {
            get { return (string)GetValue(CheckedContentProperty); }
            set { SetValue(CheckedContentProperty, value); }
        }

        public ICommand CheckedCommand
        {
            get { return (ICommand)GetValue(CheckedCommandProperty); }
            set { SetValue(CheckedCommandProperty, value); }
        }

        public Style CheckedStyle
        {
            get { return (Style)GetValue(CheckedStyleProperty); }
            set { SetValue(CheckedStyleProperty, value); }
        }

        protected override void OnToggle()
        {
            base.OnToggle();
            if (this.IsChecked.HasValue && this.IsChecked.Value)
            {                
                this.uncheckedContent = this.Content;
                this.uncheckedCommand = this.Command;
                this.uncheckedStyle = this.Style;

                object content = this.Content;

                if (this.CheckedStyle == null)
                {
                    this.Content = this.CheckedContent;
                }
                else
                {
                    this.Style = this.CheckedStyle;
                }
                
                this.Command = this.CheckedCommand;      
            }
            else
            {
                object content = this.Content;
                this.Style = this.uncheckedStyle;

                if (this.CheckedStyle == null)
                {
                    this.Content = this.uncheckedContent;
                }
                else
                {
                    this.Style = this.uncheckedStyle;
                }

                this.Command = this.uncheckedCommand;
            }
        }
    }
}

